/*
 * Copyright 2014 Rivaldo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jety.commons.util;

import com.google.gson.JsonObject;
import org.jety.commons.exception.JsonRpcRemoteException;

/**
 * Utility class to build error messages.
 * @author Rivaldo
 */
public class JsonRpcErrorBuilder {
    
    private static final String REQUEST_PARSE_ERROR = "json.rpc.request.parse";
    private static final String INVALID_REQUEST_ERROR = "json.rpc.invalid.request";
    private static final String INVALID_METHOD_ERROR = "json.rpc.invalid.method";

    /**
     * Build a json rpc request parse error message.
     * @param exception the exception that generated the error
     * @return a string with the error coded in json
     */
    public static String getRequestParseError(Throwable exception) {
        int errorCode = JsonRpcConstants.PARSE_ERROR_CODE;
        String errorMessage = Util.getErrorMessage(REQUEST_PARSE_ERROR);
        return buildError(null, new JsonRpcRemoteException(errorCode, errorMessage, exception));
    }

    /**
     * Build a json rpc invalid request error message.
     * @param requestID the request message id
     * @return a string with the error coded in json
     */
    public static String getInvalidRequestError(Integer requestID, Throwable exception) {
        int errorCode = JsonRpcConstants.INVALID_REQUEST_ERROR_CODE;
        String errorMessage = Util.getErrorMessage(INVALID_REQUEST_ERROR);
        return buildError(requestID, new JsonRpcRemoteException(errorCode, errorMessage, exception));
    }

    /**
     * Build a json rpc invalid method error message.
     * @param requestID the request message id
     * @return a string with the error coded in json
     */
    public static String getInvalidMethodError(Integer requestID) {
        int errorCode = JsonRpcConstants.INVALID_REQUEST_ERROR_CODE;
        String errorMessage = Util.getErrorMessage(INVALID_METHOD_ERROR);
        return buildError(requestID, new JsonRpcRemoteException(errorCode, errorMessage));
    }

    /**
     * Build a json rpc error message.
     * @param requestID the request message id
     * @param exception the exception that generated the error
     * @return a string with the error coded in json
     */
    public static String buildError(Integer requestID, JsonRpcRemoteException exception) {
        return buildError(requestID, exception.getCode(), exception.getMessage(), exception.getData());
    }

    /**
     * Build a json rpc error message.
     * @param requestID the request message id
     * @param code the error code
     * @param message the error message
     * @param data the error data
     * @return a string with the error coded in json
     */
    private static String buildError(Integer requestID, Integer code, String message, String data) {

        JsonObject resp = new JsonObject();
        resp.addProperty(JsonRpcConstants.PROPERTY_VERSION, JsonRpcConstants.JSON_VERSION);

        JsonObject error = new JsonObject();

        if (requestID != null) {
            error.addProperty(JsonRpcConstants.PROPERTY_ID, requestID);
        }

        if (code != null) {
            error.addProperty(JsonRpcConstants.PROPERTY_CODE, code);
        }

        if (message != null) {
            error.addProperty(JsonRpcConstants.PROPERTY_MESSAGE, message);
        }

        if (data != null) {
            error.addProperty(JsonRpcConstants.PROPERTY_DATA, data);
        }

        resp.add(JsonRpcConstants.PROPERTY_ERROR, error);

        return resp.toString();
    }
}